package com.jiayang.hhotel.presenter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.InfoModel;
import com.jiayang.hhotel.net.Api;
import com.jiayang.hhotel.ui.AccountsActivity;
import com.jiayang.hhotel.ui.AuditActivity;
import com.jiayang.hhotel.ui.SameActivity;
import com.jiayang.hhotel.util.CommonUtil;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.util.ToastUtil;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;

/**
 * Created by Administrator on 2017/5/25 0025.
 * 查账逻辑类
 */

public class AuditPresenter extends XPresent<AuditActivity> {

    /**
     * 冲销帐和转账保存请求
     * @param mpLanguage 语言
     * @param mpGstacct 客户账户
     * @param mpTrncode 账项
     * @param mpAmount 金额
     * @param mpComment 备注
     */
    public void chongXiaoZhang(int mpLanguage,long mpGstacct,int mpTrncode,double mpAmount,String mpComment){
        Api.getHomeSer().chongxiaozhang(Constant.PHONE,mpLanguage,mpGstacct,mpTrncode,mpAmount,mpComment)
                .compose(XApi.<InfoModel>getApiTransformer())
                .compose(XApi.<InfoModel>getScheduler())
                .compose(getV().<InfoModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<InfoModel>() {
                    @Override
                    protected void onFail(NetError error) {
                        CommonUtil.dismissProgressDialog(getV());
                        ToastUtil.Toast(getV().getString(R.string.net_error),getV());
                    }

                    @Override
                    public void onNext(InfoModel Model) {
                        CommonUtil.dismissProgressDialog(getV());
                        switch (Model.getState()) {
                            case 9://请求成功
                                WriteOffAccountDialog.dismiss();
                                ToastUtil.Toast(getV().getString(R.string.already_submit),getV());
                                break;
                            case -1://失败
                                ToastUtil.Toast(getV().getString(R.string.error_code)+Model.getState()+getV().getString(R.string.space)+getV().getString(R.string.service_error_contract_manager),getV());
                                break;
                        }
                    }
                });
    }

    AlertDialog WriteOffAccountDialog;
    EditText account;
    /**
     * 创建冲销帐和转账对话框
     * @param context 上下文
     * @param is true：冲销帐；false：转账
     */
    public void createLineRoomDialog(final Context context, final boolean is){
        final AlertDialog.Builder builder=new AlertDialog.Builder(context);
        View itemView= LayoutInflater.from(context).inflate(R.layout.dialog_write_off_account,null);
        account= (EditText) itemView.findViewById(R.id.et_account);//账项
        final EditText money= (EditText) itemView.findViewById(R.id.et_money);//金额
        final TextView title= (TextView) itemView.findViewById(R.id.tv_title);//标题
        if(!is){
            title.setText(context.getString(R.string.transfer_accounts));
        }else{
            title.setText(context.getString(R.string.write_off_account));
        }
        final EditText remarks= (EditText) itemView.findViewById(R.id.et_remarks);//备注
        Button enterMine= (Button) itemView.findViewById(R.id.bt_entermine);
        account.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, SameActivity.class);
                intent.putExtra(Constant.INTENT_STRING_KEY,"账项");
                ((Activity)context).startActivityForResult(intent,Constant.GUEST_ROOM_THING_REQUEST_CODE);
            }
        });
        enterMine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String accountStr=account.getText().toString();
                String moneyStr=money.getText().toString();
                String remarksStr=remarks.getText().toString();
                if(StringUtil.isEmpty(accountStr)) {
                    ToastUtil.Toast(context.getResources().getString(R.string.please_input_account), context);
                }else if(StringUtil.isEmpty(moneyStr)){
                    ToastUtil.Toast(context.getResources().getString(R.string.please_input_money), context);
                }else{
                    getV().saveAccount(Double.valueOf(moneyStr),(Integer) account.getTag(),remarksStr,is);
                }
            }
        });
        builder.setView(itemView);
        WriteOffAccountDialog = builder.show();
    }

    //获取输入账项显示控件
    public EditText getAccount(){
        return account;
    }
}
